package com.be.beadmin.basicData.controller;

import com.alibaba.fastjson.JSONArray;
import com.be.beadmin.basicData.dto.input.RoleDto;
import com.be.beadmin.basicData.dto.input.RoleMenusDto;
import com.be.beadmin.basicData.service.RoleService;
import com.be.beadmin.common.entity.IdDto;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Api(tags = "系统权限模块")
@RestController
public class RoleController {

    @Autowired
    RoleService roleService;

    @ApiOperation("获取所有角色")
    @PostMapping(value = "/system/getRoleByPage")
    public ResultModel getUserByPage(@RequestBody PageEntity pageEntity) {
        return  ResultModel.success(roleService.getRoleByPage(pageEntity)) ;
    }

    @ApiOperation("增加角色")
    @PostMapping("/system/addRole")
    public ResultModel addRole(@RequestBody RoleDto roleDto){
        roleService.addRole(roleDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("删除角色")
    @PostMapping("/system/delRole")
    public ResultModel delRole(@RequestBody RoleDto roleDto){
        roleService.delRole(roleDto);
        return ResultModel.success(ResultModel.DELETE);
    }


    @ApiOperation("获取所有角色列表")
    @PostMapping("/system/getRoleList")
    public ResultModel getRoleList(){
        roleService.getRoleList();
        return ResultModel.success(roleService.getRoleList());
    }

    @ApiOperation("更新角色")
    @PostMapping("/system/updateRole")
    public ResultModel updateRole(@RequestBody RoleDto roleDto){
        roleService.updateRole(roleDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("保存菜单")
    @PostMapping("/system/saveMenus")
    public ResultModel delRole(@RequestBody RoleMenusDto roleMenusDto){
        roleService.saveMenus(roleMenusDto);
        return ResultModel.success(ResultModel.UPDATE);

    }

    @ApiOperation("获取权限树")
    @PostMapping("/system/getPermissionTreeByRole")
    public ResultModel getPermissionTreeByRole(@RequestBody IdDto idDto){
        JSONArray array = roleService.getMenuTreeByRole(idDto.getId());
        return ResultModel.success(array);
    }


    @ApiOperation("根据ID获取权限")
    @PostMapping("/system/getPermissionById")
    public ResultModel getPermissionById(@RequestBody IdDto idDto){
//        try{
        List<String> ids = roleService.getPermissionById(idDto.getId());
        return ResultModel.success(ids);
//        }catch (Exception e){
//            return new JsonResult(new UnimaxException(e.getMessage()));
//        }
    }



}
